package com.huixuebao.exchange.service.Impl;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.IdUtil;
import com.huixuebao.common.bean.ApiResult;
import com.huixuebao.common.enumeration.ResultCodeEnum;
import com.huixuebao.exchange.dto.SettleCodeDTO;
import com.huixuebao.exchange.entity.InsuredEntity;
import com.huixuebao.exchange.entity.SettleEntity;
import com.huixuebao.exchange.mapper.SettleMapper;
import com.huixuebao.exchange.service.SettleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class SettleServiceImpl implements SettleService {

    @Autowired
    SettleMapper settleMapper;


    @Override
    public ApiResult getSettleCode(String policyId) {
        ApiResult apiResult = new ApiResult();
        try{
            SettleEntity settleEntity = new SettleEntity();
            settleEntity.setPolicyId(policyId);
            settleEntity.setId(IdUtil.simpleUUID());
            settleEntity.setCreateTime(new DateTime());
            settleEntity.setSettleCode(generateCode());
            settleMapper.insert(settleEntity);

            SettleCodeDTO settleCodeDTO = new SettleCodeDTO();
            settleCodeDTO.setSettleCode(settleEntity.getSettleCode());
            apiResult.setData(settleCodeDTO);
            apiResult.setSuccess(true);
            apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
            apiResult.setMessage("生成授权码信息成功!!");
        }catch (Exception e){
            apiResult.setSuccess(false);
            apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
            apiResult.setMessage("生成授权码信息失败!!!");
        }
        return apiResult;
    }

    /**
     * 生成6位数授权码
     * @return
     */
    private String generateCode() {
        Long codeL = System.nanoTime();
        String codeStr = Long.toString(codeL);
        String verifyCode = codeStr.substring(codeStr.length() - 6);
        return verifyCode;
    }
}
